﻿
using System;
using System.Xml.Linq;
using AzukiMap.MapSource.Vector;
namespace AzukiMap.MapSource
{
    public class PostGisSource : VectorSource
    {
        /*
        f_table_catalog   | character varying(256) | not null
        f_table_schema    | character varying(256) | not null
        f_table_name      | character varying(256) | not null
        f_geometry_column | character varying(256) | not null
        coord_dimension   | integer                | not null
        srid              | integer                | not null
        type              | character varying(30)  | not null
        */

        string _dbName;
        string _tableName;
        string _geometryColumn;
        string _tableCatalog;
        string _tableSchema;
        string _getColumnDataUri;

        public string DbName
        {
            get { return _dbName; }
            set { _dbName = value; }
        }

        public string GeometryColumn
        {
            get { return _geometryColumn; }
            set { _geometryColumn = value; }
        }

        public string TableName
        {
            get { return _tableName; }
            set { _tableName = value; }
        }

        public string TableCatalog
        {
            get { return _tableCatalog; }
            set { _tableCatalog = value; }
        }

        public string TableSchema
        {
            get { return _tableSchema; }
            set { _tableSchema = value; }
        }

        public string GetColumnDataUri
        {
            get { return _getColumnDataUri; }
            set { _getColumnDataUri = value; }
        }

        public PostGisSource(string name, VectorSourceTypes type, int srid, CoordinationUnits coordUnit)
            : base(name, type, srid, coordUnit)
        {
            init();
        }

        public PostGisSource()
            : base("PostGIS", VectorSourceTypes.Point, AzukiMapSettings.DEFAULT_SRID, CoordinationUnits.DEGREES)
        {
            init();
        }

        public void init()
        {
            _dbName = "";
            _tableName = "";
            _geometryColumn = "";
            _tableCatalog = "";
            _tableSchema = "";
            _getColumnDataUri = "";
        }

        public bool Compare(PostGisSource other)
        {
            if (_type == other._type &&
                _dbName == other._dbName &&
                _geometryColumn == other._geometryColumn &&
                _srid == other._srid &&
                _tableName == other._tableName &&
                _tableCatalog == other._tableCatalog &&
                _name == other._name &&
                _tableSchema == other._tableSchema)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public override XElement ToXml()
        {
            return ToXml("PostGisSource");
        }

        public override XElement ToXml(string elementName)
        {
            //XElement vectorSourceXml = base.ToXml();
            XElement pgSourceXml = base.ToXml(elementName);
            pgSourceXml.Add(
                _xmlCommon.CreateAttr("DbName", _dbName),
                _xmlCommon.CreateAttr("GeometryColumn", _geometryColumn),
                _xmlCommon.CreateAttr("TableName", _tableName),
                _xmlCommon.CreateAttr("TableCatalog", _tableCatalog),
                _xmlCommon.CreateAttr("TableSchema", _tableSchema),
                _xmlCommon.CreateAttr("GetColumnDataUri", _getColumnDataUri)
                );

            return pgSourceXml;
        }

        public override void Parse(XElement mapSourceXml)
        {
            base.Parse(mapSourceXml);
            XElement vectorSourceXml = mapSourceXml.Element("VectorSource");
            XElement pgSourceXml = vectorSourceXml.Element("PostGisSource");
            _dbName = _xmlCommon.GetStringAttr(pgSourceXml, "DbName");
            _geometryColumn = _xmlCommon.GetStringAttr(pgSourceXml, "GeometryColumn");
            _tableName = _xmlCommon.GetStringAttr(pgSourceXml, "TableName");
            _tableCatalog = _xmlCommon.GetStringAttr(pgSourceXml, "TableCatalog");
            _tableSchema = _xmlCommon.GetStringAttr(pgSourceXml, "TableSchema");
            _getColumnDataUri = _xmlCommon.GetStringAttr(pgSourceXml, "GetColumnDataUri");
            /*
            _dbName = pgSourceXml.Element("DbName").Value;
            _geometryColumn = pgSourceXml.Element("GeometryColumn").Value;
            _tableName = pgSourceXml.Element("TableName").Value;
            _tableCatalog = pgSourceXml.Element("TableCatalog").Value;
            _tableSchema = pgSourceXml.Element("TableSchema").Value;
            _getColumnDataUri = pgSourceXml.Element("GetColumnDataUri").Value;
             */
        }

    }
}
